package com.rockcent.message.helper;

import com.rockcent.mall.lib.domain.SysPermission;
import com.rockcent.mall.lib.domain.UserInfo;
import com.rockcent.mall.lib.dto.MallMerchantDto;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.mall.lib.helper.BaseUserHelper;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by HK on 16/9/18.
 */
@Component
public class UserHelper extends BaseUserHelper {

    /**
     * 通过明文的密码，获取md5后的密码
     * @param lawsPassword
     * @return
     */
    public String getMd5Password(String passwordTimeStamp, String lawsPassword) {
        return DigestUtils.md5Hex(UserInfo.SSO_PASSWORD_ENCODE_KEY + passwordTimeStamp + lawsPassword);
    }

    /**
     * 在 session 获取权限列表
     * @param userInfoDto
     * @return
     */
    public List<Map<String, String>> getPermissionList(UserInfoDto userInfoDto) {
        List<Map<String, String>> permissionList = new ArrayList<>();
        if(userInfoDto==null) {
            return permissionList;
        }
        Map<String, List<MallMerchantDto>> map = userInfoDto.getPermissionMap();
        if(map!=null && map.size()>0) {
            for(Map.Entry<String, List<MallMerchantDto>> entry:map.entrySet()){
                List<MallMerchantDto> list = entry.getValue();
                if(list!=null && list.size()>0) {
                    Map<String, String> permission = new LinkedHashMap<>();
                    permission.put("mallName", list.get(0).getMallName());
                    permission.put("mallBigLogoPath", list.get(0).getMallBigLogoPath());
                    permission.put("mallLogoPath", list.get(0).getMallLogoPath());
                    for (MallMerchantDto mallMerchantDto:list) {
                        if(StringUtils.isNotEmpty(mallMerchantDto.getMallUrl())) {
                            permission.put("mallUrl", mallMerchantDto.getMallUrl());
                        }
                        if(StringUtils.isNotEmpty(mallMerchantDto.getMerchantUrl())) {
                            permission.put("merchantUrl", mallMerchantDto.getMerchantUrl());
                        }
                    }
                    permissionList.add(permission);
                }
            }
        }
        return permissionList;
    }

}
